package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbCusTerDao;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.exception.RRException;
import com.framework.service.yhbservice.*;
import com.framework.validator.Assert;
import com.framework.yhbEntity.user.YhbCusTer;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.user.YhbUserTerminalRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
@Transactional(rollbackFor = Exception.class)
@Service("yhbCusTerService")
public class YhbCusTerServiceImpl extends ServiceImpl<YhbCusTerDao,YhbCusTer> implements YhbCusTerService {

    @Autowired
    private YhbTerminalService yhbTerminalService;

    @Autowired
    private YhbUserService yhbUserService;
    @Autowired
    private YhbUserTerminalRelationService yhbUserTerminalRelationService;

    @Autowired
    private ValidateService validateService;

    @Override
    public Map<String,Object> queryInfoBySn(String sn) {

        Map<String,Object> custer = baseMapper.queryInfoBySn(sn);

        return custer;
    }

    @Override
    public YhbCusTer queryInfoByPhone(String phone, Long deptId) {
        //List<YhbCusTer> list = baseMapper.selectList(new EntityWrapper<YhbCusTer>().eq("phone", phone).eq("dept_id", deptId));
        YhbCusTer t=new YhbCusTer();
        t.setPhone(phone);
        t.setDeptId(deptId);
        t.setIsDel(0);
        YhbCusTer cusTer = baseMapper.selectOne(t);
        return cusTer;
    }

    @Override
    public YhbCusTer applyCusTerminal(String username, String phone, String sn) {

        TerTerminalInfo t = yhbTerminalService.queryTerminalInfoBySn(sn);
        if(t==null){
            throw new RRException("9000","未查找到此终端");
        }
        if(t.getIsTradition().intValue()==0){
            applyBaiYiTerminal(t,phone);
        }

        //验证是否申请过
        YhbCusTer yhbcusTer=queryOneBySnAndPhone(phone,sn);
        if(yhbcusTer!=null){

            switch (yhbcusTer.getState().intValue()){
                case 1:
                    throw new RRException("9000","您已绑定该终端,请勿重复绑定");
                case 2:
                    throw new RRException("9000","您已申请该终端,请等待审核");
                case 3:
                    throw new RRException("9000","您已申请该终端结果未失败,请修改后发送申请");
            }
        }


        //验证其他人是否申请过
        YhbCusTer yct = queryOneBySn(sn);
        if(yct!=null){
            throw new RRException("9000","该终端已被他人申请");
        }

        YhbCusTer yhbCusTer=new YhbCusTer();

        yhbCusTer.setPhone(phone);
        Long deptId=t.getDeptId();
        if(deptId.equals(1L)){
            deptId=2L;
        }
        yhbCusTer.setDeptId(deptId);
        yhbCusTer.setSn(sn);
        yhbCusTer.setModel(t.getModel());
        yhbCusTer.setState(2);
        yhbCusTer.setTerminalType(t.getTerminalType());
        yhbCusTer.setUsername(username);
        yhbCusTer.setCreateDate(new Date());
        yhbCusTer.setUpdateDate(new Date());
        baseMapper.insert(yhbCusTer);
        YhbCusTer c=new YhbCusTer();
        c.setPhone(phone);
        c.setSn(sn);
        YhbCusTer ct = baseMapper.selectOne(c);
        if (ct==null) {
            throw new RRException("9000","申请失败");
        }
        //判断是否是已注册用户申请，是则添加联系，并且非使用状态
        YhbUser user = yhbUserService.queryUserByPhone(phone, deptId);
        if(user!=null){
            YhbUserTerminalRelation relation=new YhbUserTerminalRelation();
            relation.setIsUse(0);
            relation.setSn(sn);
            relation.setUserId(user.getUserId());
            relation.setCreateDate(new Date());
            yhbUserTerminalRelationService.insert(relation);
        }

        return ct;
    }

    private void applyBaiYiTerminal(TerTerminalInfo t, String phone) {
        Long deptId=2L;
        YhbUser user = yhbUserService.queryUserByPhone(phone, deptId);

        if(user==null){
            throw new RRException("9000","该手机号未注册");
        }
        YhbUserTerminalRelation relation=new YhbUserTerminalRelation();
        relation.setIsUse(1);
        relation.setSn(t.getSn());
        relation.setUserId(user.getUserId());
        relation.setCreateDate(new Date());
        yhbUserTerminalRelationService.insert(relation);

        t.setIsYhbuse(1);
        yhbTerminalService.updateById(t);

    }


    @Override
    public YhbCusTer queryOneBySnAndPhone(String phone, String sn) {
        YhbCusTer t=new YhbCusTer();
        t.setSn(sn);
        t.setPhone(phone);
        t.setIsDel(0);
        return  baseMapper.selectOne(t);

    }

    @Override
    public YhbCusTer queryOneBySn(String sn) {
        YhbCusTer t=new YhbCusTer();
        t.setSn(sn);
        t.setIsDel(0);
        YhbCusTer yct = baseMapper.selectOne(t);
        return yct;
    }

    @Override
    public YhbCusTer updateCusTerminal(String username, String phone, String sn, String yhbCusTerId, Integer terminalType) {

        TerTerminalInfo t = yhbTerminalService.queryTerminalInfoBySn(sn);
        validateService.validateSn(sn,2L, terminalType);

        YhbCusTer yct = baseMapper.selectById(yhbCusTerId);

        if(yct==null){
            throw new RRException("9000","未查询到申请信息");
        }

        yct.setPhone(phone);
        yct.setSn(sn);
        yct.setUsername(username);
        yct.setTerminalType(t.getTerminalType());
        yct.setModel(t.getModel());
        yct.setState(2);
        yct.setUpdateDate(new Date());
        baseMapper.updateById(yct);

        yct = baseMapper.selectById(yhbCusTerId);
        return yct;
    }

    @Override
    public void applyBaiYiTerminal(String token, String sn) {
        Assert.isBlank(sn,"终端号不能为空");
        YhbUser user = yhbUserService.queryUserByToken(token);
        YhbUserTerminalRelation relation=new YhbUserTerminalRelation();
        relation.setCreateDate(new Date());
        relation.setUserId(user.getUserId());
        relation.setIsUse(1);
        relation.setSn(sn);
        yhbUserTerminalRelationService.insert(relation);
        TerTerminalInfo t = yhbTerminalService.queryTerminalInfoBySn(sn);
        t.setIsYhbuse(1);
        yhbTerminalService.updateById(t);
    }

    @Override
    public void cancelTerminal(String sn) {
        YhbCusTer t = queryOneBySn(sn);
        if(t==null){
            throw new RRException("9000","未查询到该终端的申请信息");
        }
        t.setIsDel(1);
        baseMapper.updateById(t);
    }
}
